London | 25-ITP-Sep | KME Blair | Sprint 3 | Implement and Rewrite Tests Coursework #861
London | 25-ITP-Sep | KME Blair | Sprint 3 | Implement and Rewrite Tests Coursework #861kohanman wants to merge 12 commits intoCodeYourFuture:mainfrom
Conversation
| function getAngleType(angle) { | ||
| if (angle === 90) { | ||
| return "Right angle"; | ||
| } else if (angle === 45) { | ||
| return "Acute angle"; | ||
| } else if (angle === 120) { | ||
| return "Obtuse angle"; | ||
| } else if (angle === 180) { | ||
| return "Straight angle"; | ||
| } else if (180 < angle < 360) { | ||
| return "Reflex angle"; | ||
| } |
There was a problem hiding this comment.
-
You misunderstood the definition of these terms. Please lookup the definition of these angles and update your function accordingly.
-
You should also test the function on angle larger than or equal to 360.
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| let rank = card.charAt(0); |
There was a problem hiding this comment.
Does your function return the value you expected from each of the following function calls?
getCardValue("11♠");
getCardValue("100♠");
getCardValue("0x02♠");
getCardValue("3.1416♠");
getCardValue("0002♠");
getCardValue("2e0♠");
| test("should return true for an negative fraction", () => { | ||
| expect(isProperFraction(-4, 7)).toEqual(true); | ||
| }); |
There was a problem hiding this comment.
The test description is misleading because not all negative fractions are proper fraction.
For example, -7/4 is not a proper fraction.
|
|
||
| // Case 2: Identify Improper Fractions: | ||
| test("should return false for an improper fraction", () => { | ||
| expect(isProperFraction(5, 2)).toEqual(false); |
There was a problem hiding this comment.
We can write multiple expect(isProperFraction(..., ...)).toEqual(false) with different parameters to test different improper fractions in one test to make the test more complete.
| }); | ||
|
|
||
| test("should return 10 for Face Cards", () => { | ||
| const faceCards = getCardValue("10" || "J" || "Q" || "K"); |
There was a problem hiding this comment.
- Can you find out from AI what
"10" || "J" || "Q" || "K"evaluates to in JS?
Note: 10 is considered a number card, not a face card.
| test("should return 5 for 5 of Hearts", () => { | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| expect(fiveofHearts).toEqual(5); | ||
| }); |
There was a problem hiding this comment.
-
The function is not expected to check if the last character is a valid suit character, so we don't have to name the suit character ("Heart").
-
When preparing tests, we should ensure the tests cover all possible cases. If we specify a test for individual card, we will need about 53 tests to cover all possible cases. Instead, we could consider classifying all possible values into different categories, and then within each category we test some samples.
For example, one possible category for getCardValue() is, "should return the value of number cards (2-10)", and we can prepare the test as
test("should return the value of number cards (2-10)", () => {
expect(getCardValue("2♣︎")).toEqual(2);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("10♥")).toEqual(10);
// Note: We could also use a loop to check all values from 2 to 10.
});
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
|
|
||
| test('should return "2nd" for 2', () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| }); | ||
|
|
||
| test('should return "13th" for 13', () => { | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| }); | ||
|
|
||
| test("should return '41st' for 41", () => { | ||
| expect(getOrdinalNumber(41)).toEqual("41st"); | ||
| }); | ||
|
|
||
| test('should return "212th" for 212', () => { | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| }); | ||
|
|
||
| test("should return '221st' for 221", () => { | ||
| expect(getOrdinalNumber(221)).toEqual("221st"); | ||
| }); |
There was a problem hiding this comment.
To ensure thorough testing, we need broad scenarios that cover all possible cases.
Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories.
Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.
For example, we can prepare a test for numbers 2, 22, 132, etc. as
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});
| if (count < 0) { | ||
| return "Error, negative integer"; | ||
| } |
There was a problem hiding this comment.
How would the caller distinguish the result of the following two function calls?
repeat("Error, negative integer", 1)repeat("", -1)
Both function calls return the same value.
| test("should throw an error if count is negative", () => { | ||
| const str = "hello"; | ||
| const count = -1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("Error, negative integer"); | ||
| }); |
There was a problem hiding this comment.
If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)
There was a problem hiding this comment.
Are the files in prep folder related to Sprint-3 exercise? If not, you should remove them to keep the branch clean.
Learners, PR Template
Self checklist
Changelist
Completed the implement and rewrite test coursework
Questions
N/A